#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

import unittest

from jute.db import Table
from jute.db import RowMutator

from jute.db import Column
from jute.db import ColumnFamily

class   Table01TestCase(unittest.TestCase) :
    def setUp(self) :
        fp = file('schema.xml')
        self.schema_xml = fp.read()
        fp.close()
        ## setUp()

    def tearDown(self) :
        pass
        ## tearDown()

    def testSchema01(self) :
        table_page = Table.open('page', self.schema_xml)
        self.assertFalse(table_page is None)

        cf_names = table_page.getColumnFamilyNames()
        self.assertEqual(2, len(cf_names))
        self.assertTrue('data' in cf_names)
        self.assertTrue('attr' in cf_names)

        self.assertEqual(1, table_page.getColumnFamilyId('data'))
        self.assertEqual(2, table_page.getColumnFamilyId('attr'))
        self.assertEqual('data', table_page.getColumnFamilyName(1))
        self.assertEqual('attr', table_page.getColumnFamilyName(2))

        table_cache = Table.open('cache', self.schema_xml)
        self.assertFalse(table_cache is None)

        cf_names = table_cache.getColumnFamilyNames()
        self.assertEqual(1, len(cf_names))
        self.assertTrue('data' in cf_names)

        self.assertEqual(1, table_cache.getColumnFamilyId('data'))
        self.assertEqual(0, table_cache.getColumnFamilyId('attr'))
        self.assertEqual('data', table_cache.getColumnFamilyName(1))
        self.assertEqual(None, table_cache.getColumnFamilyName(2))
        ## testSchema01()

    def testRowMutator01(self) :
        table_page = Table.open('page', self.schema_xml)
        self.assertFalse(table_page is None)

        mutator = table_page.getRowMutator('key1')
        self.assertEqual(table_page, mutator.table)
        self.assertEqual('key1', mutator.row_key)
        ## testRowMutator01()

    def testRowMutator02(self) :
        table_page = Table.open('page', self.schema_xml)
        self.assertFalse(table_page is None)

        mutator = table_page.getRowMutator('row_key1')
        self.assertEqual(table_page, mutator.table)
        self.assertEqual('row_key1', mutator.row_key)

        c1 = Column('col1','val1',1)
        c2 = Column('col2','val2',2)
        c3 = Column('col3','val3',3)
        c4 = Column('col4','val4',4)
        cf = ColumnFamily( 'data' )
        for c in (c1, c2, c3, c4) :
            cf.addColumn( c )
        mutator.addColumnFamily( cf )
        mutator.apply()

        # TODO : to check we insert correctly

        ## testRowMutator02()

    ## class Table01TestCase


if __name__ == '__main__' :
    unittest.main()

